﻿#include<iostream>
using namespace std;
int main() {
	unsigned x;
	cin >> x;

	//C++
	int* pA{}, * pM{}, * pold;

	pA = new int[x];//失败返回0，底层实现用malloc
	*pA = 500;
	pA[0] = 500;

	delete(pA);
	delete[] pM;
	//new int delete p;
	//new int[x] delete[] p;

	//C
	int* p = (int*)malloc(x * sizeof(int));//malloc返回的是void类型的指针，需要强制转换
	int* pm = (int*)calloc(x, sizeof(int));//calloc初始化所有变量为0，效率没malloc高
	int* pn = (int*)realloc(p, x);//realloc是将原本已经分配好的内存复制到重新分配的内存里

	if (p == nullptr) {
		cout << "内存分配失败！";
	}
	else {
		p[0] = 9;
		p[1] = 2;
		p[2] = p[0] * p[1];
		cout << p[0] << " " << p[1] << " " << p[2] << endl;
	}

	cout << p << endl;//000001E52BFEFA30

	//free(p);

	cout << p << endl; //000001E52BFEFA30

	//p = 0;//free后的指针是悬挂指针，仍然指向原本地址，所以需要让它等于0
	free(pm); pm = 0;
	free(pn); pn = 0;

	cout << p << endl;//0000000000000000

}